<!--岗位-->
<template>
    <a-tree-select
        v-bind="$attrs"
        v-on="$listeners"
        style="width: 100%"
        :tree-data="treeData"
        :dropdown-style="{ maxHeight: '400px', overflow: 'auto' }"
        :tree-checkable="treeCheckable"
        :replaceFields="treeReplace"
        :show-checked-strategy="showCheckedStrategy"
        @treeExpand="handleTreeExpand"
        @select="handleSelect"
        @change="handleChange"
        placeholder="请选择"
    />
</template>
<script>
import Bus from '@/tool/Bus.js'
import { TreeSelect } from 'ant-design-vue'
import { getJobs } from '@/api/subassembly/TreeSelectOrganization'
const SHOW_ALL = TreeSelect.SHOW_ALL //显示所有选中节点(包括父节点).
const SHOW_PARENT = TreeSelect.SHOW_PARENT //只显示父节点(当父节点下所有子节点都选中时)
const SHOW_CHILD = TreeSelect.SHOW_CHILD //默认只显示子节点.
export default {
    name: 'Jobs',
    props: {
        /**
         * @description: 定义选中项回填的方式
         * @param {} SHOW_ALL  显示所有选中节点(包括父节点).
         * @param {} SHOW_PARENT  只显示父节点(当父节点下所有子节点都选中时)
         * @param {} SHOW_CHILD  默认只显示子节点
         */
        showCheckedStrategy: {
            default: SHOW_CHILD,
        },
        treeCheckable: {
            // 是否有复选框
            type: Boolean,
            default: false,
        },
        staff: {
            //是否请求人员列表
            type: Boolean,
            default: false,
        },
        addTitel: {
            type: String,
            default: '',
        },
        isThreeLevel:{ //可联动可单独存在
          type: Boolean,
          default: false
        },
        // onlyJobs:{ //可联动可单独存在
        //   type: Boolean,
        //   default: false
        // }
    },
    
    data() {
        return {
            SHOW_ALL,
            SHOW_PARENT,
            SHOW_CHILD,
            showSearch: true,
            treeReplace: {
                title: 'name',
                value: 'id',
                key: 'id',
            },
            treeData: [],
        }
    },
    created() {
        Bus.$off('GetJobs')
        Bus.$on('GetJobs', this.getJobs)
    },
    methods: {
        getJobs(val) {
            if (val.selected) {
                const params = {
                    orgId: val.triggerValue, // 传对应的Id
                }
                this.clearEmit()
                this.clearStaff()
                this.treeData = []
                getJobs(params).then((res) => {
                    //添加组织架构前缀
                    if (res.status === 200) {
                        if (this.addTitel != '' && this.isThreeLevel == false) {
                            res.data.forEach((e) => {
                                e.name = this.addTitel + '/' + e.name
                            })
                        }else{
                          res.data.forEach((e) => {
                            e.name = e.pname
                          })
                        }

                        this.treeData = res.data
                    }
                })
            } else {
                this.treeData = []
                this.clearEmit()
                this.clearStaff()
            }
        },
        /**
         * @description: 展开节点时调用
         * @param {object} expandedKeys 展开节点时的对象
         */
        handleTreeExpand(expandedKeys) {
            this.$emit('update:expandedKeys', expandedKeys)
        },
        /**
         * @description: 被选中时调用
         * @param {string} value 被选中时调用的id字符串
         */
        handleSelect(value, node, event) {
            this.$emit('update:selectValue', value)
            this.$emit('job-select',value,node.title)
        },
        /**
         * @description: 选中树节点时调用此函数
         * @param {object} value  选中树节点的id
         * @param {object} label  选择树节点的文本信息对象
         */
        handleChange(value, label, extra) {
            const pName = extra.triggerNode.dataRef.pname
            this.$emit('update:selectedValue', value)
            this.$emit('job-change',value,label[0])
            this.$emit('update:selectedLabel', pName)
            if (this.staff) {
                extra.haveJobs = true
                Bus.$emit('GetStaff', extra)
                this.tip(extra)
            }
        },
        /**
         * @description: 联动为单选提示
         */
        tip(extra) {
            if (extra.selected) {
            } else {
                console.log('连动时请设置为单选')
            }
        },
        clearEmit() {
            this.$emit('update:expandedKeys')
            this.$emit('update:selectValue')
            this.$emit('update:selectedValue')
            this.$emit('update:selectedLabel')
        },
        clearStaff() {
            if (this.staff) {
                const extra = {
                    haveJobs: true,
                    selected: false,
                }
                Bus.$emit('GetStaff', extra)
            }
        },
    },
}
</script>
